package com.heima.admin.controller.v1;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.heima.admin.config.WmSensitiveDataListener;
import com.heima.api.wemedia.WemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.model.wemedia.pojos.WmSensitiveExcel;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/sensitive")
@AllArgsConstructor
public class SensitiveController {

    private final WemediaClient wemediaClient;


    @ApiOperation("删除敏感词")
    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return wemediaClient.delete(id);
    }

    @ApiOperation("分页查询敏感词")
    @PostMapping("/list")
    public PageResponseResult list(@RequestBody WmSensitiveDto dto) {
        return wemediaClient.list(dto);
    }

    @ApiOperation("保存敏感词")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmSensitiveDto dto) {

        return wemediaClient.save(dto);
    }

    @ApiOperation("修改敏感词")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmSensitiveDto dto){

        return wemediaClient.update(dto);
    }

    @ApiOperation("导入敏感词")
    @PostMapping("/import")
    public ResponseResult importSensitive(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        List<WmSensitive> reqCustomerDailyImports = new ArrayList<>();// 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        // 创建一个WmSensitiveDataListener实例
        WmSensitiveDataListener listener = new WmSensitiveDataListener();
        // 使用EasyExcel的read方法读取Excel文件
        EasyExcel.read(inputStream, WmSensitive.class, listener).sheet().doRead();
        // 返回解析的数据列表
        List<WmSensitive> list = listener.getList();
        wemediaClient.saveBatch(list);
        return ResponseResult.okResult("");
    }

    @ApiOperation("导出敏感词")
    @GetMapping("/export")
    public void exportSensitive(HttpServletResponse response) throws IOException {
        // 生成数据
        List<WmSensitiveExcel> result = new ArrayList<>();
        List<WmSensitive> list = wemediaClient.findAll();
        list.forEach(item -> {
            WmSensitiveExcel excel = new WmSensitiveExcel();
            BeanUtils.copyProperties(item, excel);
            result.add(excel);
        });
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = LocalDateTime.now() + "sensitive";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WmSensitiveExcel.class)
                .sheet("sheet0")
                // 设置字段宽度为自动调整，不太精确
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(result);
    }
}

